<template>
  <w-tr>
    <template #default>
      <w-td>
        <w-leaf :text="data.name" :expanded="data.expanded" :children="data.children" :src="getSrc(data)" />
      </w-td>
      <w-td>{{data.code}}</w-td>
    </template>
    <template #rows>
      <leaf-src-tr v-for="item in data.children" :key="item.id" :data="item"></leaf-src-tr>
    </template>
  </w-tr>
</template>

<script>
  let index = 0
  
  export default {
    name: 'leaf-src-tr',
    props: ['data'],
    methods: {
      getSrc(data) {
        if (!data.hasChild) return null
        return () => new Promise(resolve => {
          setTimeout(() => resolve([
            {id: ++index, name: '子节点' + index, code: index},
            {id: ++index, name: '子节点' + index, code: index},
          ]), 300)
        }).then(res => data.children = res)
      }
    }
  }
</script>
